package com.example.japinhands

import android.content.Context
import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.SideEffect
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.core.view.WindowCompat
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.example.japinhands.data.WordConstant.SENTENCE_KEY_NOT_EXIST
import com.example.japinhands.data.WordConstant.currSentenceKey
import com.example.japinhands.data.WordConstant.saveNewKey
import com.example.japinhands.route.Route
import com.example.japinhands.ui.JapScaffold
import com.example.japinhands.ui.screen.ManageDeformAdjScreen
import com.example.japinhands.ui.screen.ManageSentenceScreen
import com.example.japinhands.ui.screen.DeformAdjScreen
import com.example.japinhands.ui.screen.DeformVerbScreen
import com.example.japinhands.ui.screen.TestManageWordScreen
import com.example.japinhands.ui.screen.TestNewSentenceScreen
import com.example.japinhands.ui.screen.TestScreen
import com.example.japinhands.ui.screen.newword.WordScreen
import com.example.japinhands.ui.theme.JapInHandsTheme
import com.google.accompanist.systemuicontroller.rememberSystemUiController

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (currSentenceKey == SENTENCE_KEY_NOT_EXIST) {
            Log.d("JapMainActivity", "First create sentence key")
            saveNewKey(0)
        }
        WindowCompat.setDecorFitsSystemWindows(window, false)
        setContent {
            // Remember a SystemUiController
            val systemUiController = rememberSystemUiController()
            val useDarkIcons = !isSystemInDarkTheme()

            JapInHandsTheme {
                SideEffect {
                    // Update all of the system bar colors to be transparent, and use
                    // dark icons if we're in light theme
                    systemUiController.apply {
                        setSystemBarsColor(
                            color = Color.Transparent,
                            darkIcons = useDarkIcons,
                            isNavigationBarContrastEnforced = false
                        )
                        setStatusBarColor(
                            color = Color.Transparent,
                            darkIcons = true
                        )
                    }
                    // setStatusBarColor() and setNavigationBarColor() also exist
                }
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
//                    JapInHandTest(context = this)
                    JapScaffold(context = this)
                }
            }
        }
    }
}



@Composable
fun JapInHandTest(
    context: Context
) {
    val navController = rememberNavController()
    val sharedViewModel: JapViewModel = viewModel(
//        factory = WordViewModelFactory(
//            wordDao = WordDatabase.getDatabase(context = context).wordDao(),
//            adjDao = WordDatabase.getDatabase(context = context).adjDao(),
//            verbDao = WordDatabase.getDatabase(context = context).verbDao(),
//            sentenceDao = WordDatabase.getDatabase(context = context).sentenceDao()
//        )
    )
    NavHost(
        navController = navController,
        startDestination = Route.Test.route
    ){
        composable(route = Route.Test.route) {
            TestScreen(
                navController = navController,
                viewModel = sharedViewModel
            )
        }
        composable(route = Route.NewSentence.route) {
            TestNewSentenceScreen(
                viewModel = sharedViewModel
            )
        }
        composable(route = Route.ManageSentence.route) {
            ManageSentenceScreen(
                viewModel = sharedViewModel
            )
        }
        composable(route = Route.NewDeformVerb.route) {
            DeformVerbScreen(
                viewModel = sharedViewModel,
                update = false
            )
        }
        composable(route = Route.NewDeformAdj.route) {
            DeformAdjScreen(
                viewModel = sharedViewModel,
                update = false
            )
        }
        composable(route = Route.ManageDeformAdj.route) {
            ManageDeformAdjScreen(
                viewModel = sharedViewModel
            )
        }
        composable(route = Route.NewWord.route) {
            WordScreen(
                viewModel = sharedViewModel,
                update = false
            )
        }
        composable(route = Route.TestManageWord.route) {
            TestManageWordScreen(viewModel = sharedViewModel)
        }
    }
}


@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
    Text(
        text = "Hello $name!",
        modifier = modifier
    )
}

@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
    JapInHandsTheme {
        Greeting("Android")
    }
}